var express = require('express');
var router = express.Router();
var purchaseDao = require('../dao/purchase');
var AdminToken = require('../db/user_tokens');
var purchaseRoutes = module.exports;

/**
 * 添加/编辑购买项
 * @param  id
 * @param  name
 * @param  price
 * @param  duration   (天)
 * @param  lvl
 * @param  multi_rate
 * @param  status
 *         url/purchase/save
 */
purchaseRoutes.save = function(req, res) {
    var par = paramAll(req);
    if(!par.name) return res.json(new ERR('name不为空！', 400));
    if(!par.price) return res.json(new ERR('price不为空！', 400));
    if(!par.duration) return res.json(new ERR('duration不为空！', 400));
    if(!par.lvl) return res.json(new ERR('lvl不为空！', 400));
    AdminToken.checkAdminLogin(par.token, null, function(err, errCode, adminInfo) {
        if(err) return  res.json(new ERR(err, errCode));
        purchaseDao.save(par, function(err, result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        })
    })
}
router.post('/save', purchaseRoutes.save);

/**
 * 购买项列表
 * @param  token
 * @param  page
 * @param  size
 *         url/purchase/list
 */
purchaseRoutes.list = function(req, res) {
    var par = paramAll(req);
    par.size = !par.size ? 10 : parseInt(par.size);
    par.start = !par.page ? 0 : par.start = (parseInt(par.page) - 1) * parseInt(par.size);
    purchaseDao.list(par, function(err, result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    })
}
router.post('/list', purchaseRoutes.list);

/**
 * 设置购买项状态
 * @param  id
 * @param  status
 * @param  token
 *         url/purchase/updateStatus
 */
purchaseRoutes.updateStatus = function(req, res) {
    var par = paramAll(req);
    AdminToken.checkAdminLogin(par.token, null, function(err, errCode, adminInfo) {
        if(err) return  res.json(new ERR(err, errCode));
        purchaseDao.updateStatus(par, function(err, result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        })
    });

}
router.post('/updateStatus', purchaseRoutes.updateStatus);

/**
 * 收款记录
 * @param  size
 * @param  page
 * @param  token
 *         url/purchase/receivables
 */
purchaseRoutes.receivables = function(req, res) {
    var par = paramAll(req);
    par.size = !par.size ? 10 : parseInt(par.size);
    par.start = !par.page ? 0 : par.start = (parseInt(par.page) - 1) * parseInt(par.size);
    AdminToken.checkAdminLogin(par.token, null, function(err, errCode, adminInfo) {
        if(err) return  res.json(new ERR(err, errCode));
        purchaseDao.receivables(par, function(err, result) {
            if(err) return res.json(new ERR(err, 400));
            res.json(new PKG(result));
        })
    });
}
router.post('/receivables', purchaseRoutes.receivables);

/**
 * 获取购买项信息
 * @param  id
 *         url/purchase/getPurchaseInfo
 */
purchaseRoutes.getPurchaseInfo = function(req, res) {
    var par = paramAll(req);
    if(!par.id) return res.json(new ERR('购买项信息不为空！', 400));
    purchaseDao.getPurchaseInfo(par, function(err, result) {
        if(err) return res.json(new ERR(err, 400));
        res.json(new PKG(result));
    })
}
router.post('/getPurchaseInfo', purchaseRoutes.getPurchaseInfo);

module.exports = router;